<?php
// Module Body

if (!function_exists('bw_quota_warn')) {
function bw_quota_warn(&$data) {
	global $T, $cfg;

	$T['transferquotasubject'] = "Transfer Quota Limit Exceeded";
	$T['domaintransferbody'] = "Please note that your domain: %DOMAIN% has exceeded the transfer limit set by your System Administrator. You have used %TRANSFERUSED%GB transfer with only %TRANSFER%GB allocated. You will receive this email daily whenever your transfer quota is exceeded. You may check your transfer quota from the DomainCP page in WebCP. Please contact your System Administrator for more information.";
	$T['resellertransferbody'] = "Please note that your reseller account: %NAME% has exceeded the transfer limit set by your System Administrator. Your domains have used %TRANSFERUSED%GB transfer with only %TRANSFER%GB allocated. You will receive this email daily whenever your transfer quota is exceeded. You may check your transfer quota from the ResellerCP page in WebCP. Please contact your System Administrator for more information.";	

	if ($cfg['sendquotaemails']) {
		// Start domain transfer check
		$dbp = mysql_query("SELECT domains.email, transfertbl.value as transfer, transferusedtbl.value as transfer_used, domains.domain FROM domains INNER JOIN properties as transfertbl ON domains.id = transfertbl.id INNER JOIN properties as transferusedtbl ON domains.id = transferusedtbl.id WHERE transfertbl.name = 'transfer' AND transferusedtbl.name = 'transfer_used' AND transfer_used >= transfer AND transfer != -1 AND domains.type = 'domain' AND domains.suspended = 'false'");
		
		while ($data = mysql_fetch_array($dbp)) {
			$search[] = "%DOMAIN%";
			$search[] = "%TRANSFERUSED%";
			$search[] = "%TRANSFER%";
			$replace[] = $data['domain'];
			$replace[] = $data['transfer_used'];
			$replace[] = $data['transfer'];
			$body = str_replace($search, $replace, $T['domaintransferbody']);
			mail($data["email"], $T['transferquotasubject'], $body, "From: noreply@".$cfg['sysname']);
			webcp_log(2,0,'quotawarn',"domain transfer quota warning sent to ".$data['domain'],0);
		}
		
		// Start reseller transfer check
		$dbp = mysql_query("SELECT resellers.name, resellers.email, sum(transferusedtbl.value) AS transferused, transfertbl.value AS transfer FROM resellers inner join domains on resellers.id = domains.owner INNER JOIN properties as transfertbl ON resellers.id = transfertbl.id INNER JOIN properties as transferusedtbl ON domains.id = transferusedtbl.id WHERE transfertbl.name = 'transfer' AND transferusedtbl.name = 'transfer_used' AND transferused >= transfer AND resellers.transfer != -1 AND resellers.hold = 'false' GROUP BY resellers.name");
		
		while ($data = mysql_fetch_array($dbp)) {
			if ($data["transferused"] > $data["transfer"]) {
				$search[] = "%NAME%";
				$search[] = "%TRANSFERUSED%";
				$search[] = "%TRANSFER%";
				$replace[] = $data['name'];
				$replace[] = $data['transferused'];
				$replace[] = $data['transfer'];
				$body = str_replace($search, $replace, $T['resellertransferbody']);
				mail($data["email"], $T['transferquotasubject'], $body, "From: noreply@".$cfg['sysname']);
				webcp_log(2,0,'quotawarn',"reseller transfer quota warning sent to ".$data['name'],0);
			}
		}
	}
}
}

if (!function_exists('bw_quota_reset')) {
function bw_quota_reset(&$data) {
	// Clean up transfer used
	$SQL = 'UPDATE properties SET value = 0 WHERE name = "transfer_used"';
	mysql_query($SQL);
}
}
?>
